#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include <time.h>
sem_t codeIn;
//产生输入
void* fun1(void* arg)
{
    while(1)
    {
        char buf[128];
        fgets(buf,128,stdin);
        sem_post(&codeIn);
    }

}
//输出
void *fun2(void* arg)
{
    while(1)
    {
        struct timespec tm;
        clock_gettime(CLOCK_REALTIME,&tm);
        tm.tv_sec +=5;
        int ret = sem_timedwait(&codeIn,&tm);
        if(ret)
        {
            //超时了
            printf("hello world out of time\n");
        }
        else {
            printf("HELLO WORLD IN TIME\n");

        }
    }

}

int main02()
{
    pthread_t p1,p2;
    pthread_create(&p1,NULL,fun1,NULL);
    pthread_create(&p2,NULL,fun2,NULL);

    sem_init(&codeIn,0,0);

    pthread_join(p1,NULL);
    pthread_join(p2,NULL);

    sem_destroy(&codeIn);
    return 0;
}
